# CMake build script for the SimpleGit project
#
# Building (out of source build):
# > mkdir build && cd build
# > cmake .. [-DSETTINGS=VALUE]
# > cmake --build .
#
# Install:
# > cmake --build . --target install

SET(CMAKE_SYSTEM_NAME "Generic")

project (SimpleGit C)
cmake_minimum_required (VERSION 2.6)


set(ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}/..)
set(SRC_DIRECTORY ${ROOT_DIRECTORY}/src)

include_directories(
	${SRC_DIRECTORY}
	${SRC_DIRECTORY}/common
	${SRC_DIRECTORY}/common/utils
	${SRC_DIRECTORY}/common/commands
	${SRC_DIRECTORY}/builtin
	#place new include directories here
)

IF (NOT LIBGIT2_BUILD_DIRECTORY_NAME)
	SET(LIBGIT2_BUILD_DIRECTORY_NAME build)
ENDIF()

set(TESTS_DIRECTORY ${ROOT_DIRECTORY}/tests)
set(LIBGIT2_DIRECTORY ${ROOT_DIRECTORY}/libgit2)
set(LIBGIT2_BUILD_DIRECTORY ${LIBGIT2_DIRECTORY}/${LIBGIT2_BUILD_DIRECTORY_NAME})
set(BINARIES_DIRECTORY ${ROOT_DIRECTORY}/bin)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
set(EXECUTABLE_OUTPUT_PATH ${BINARIES_DIRECTORY})
set(EXECUTABLE_SOURCE_FILE ${SRC_DIRECTORY}/git.c)
set(UNIT_TESTS_DIRECTORY ${ROOT_DIRECTORY}/unittests)

file(GLOB_RECURSE C_SOURCE_FILES ${SRC_DIRECTORY}/*.c)
list(REMOVE_ITEM C_SOURCE_FILES ${EXECUTABLE_SOURCE_FILE})

#add_definitions(-DNO_PTHREADS)

SET(CMAKE_C_FLAGS "-Wno-missing-field-initializers -std=gnu99 -D_POSIX_C_SOURCE=200809L -Wextra ${CMAKE_C_FLAGS}")

set(CMAKE_C_FLAGS_DEBUG "-g -O1")
set(CMAKE_C_FLAGS_RELEASE "-O2")

set(OPENSSL_USE_STATIC_LIBS TRUE)
set(OPENSSL_SSL_LIBRARY ssl)
set(OPENSSL_CRYPTO_LIBRARY crypto)
find_package(OpenSSL REQUIRED)

message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")

include_directories(${OPENSSL_INCLUDE_DIR})
#list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})

#include libssl
#find_package(OpenSSL)
#add_definitions(-DOPENSSL_SHA1 -DNO_OPENSSL -DGIT_WIN32)

#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)

include_directories(${INCLUDE_DIRECTORIES})
include_directories(${LIBGIT2_DIRECTORY}/include)
include_directories(${LIBGIT2_DIRECTORY}/src)

link_directories(${OPENSSL_INCLUDE_DIR}/../lib/mingw/x64)
link_directories(${LIBGIT2_BUILD_DIRECTORY})
set(LIBS ${LIBS} git2 ${LIB_SHA1})
set(LIBS ${LIBS} ssl crypto)


add_library(sgit-lib STATIC ${C_SOURCE_FILES})
add_executable(sgit-bin ${EXECUTABLE_SOURCE_FILE})
set_target_properties(sgit-bin PROPERTIES OUTPUT_NAME sgit)
set_target_properties(sgit-bin PROPERTIES LINK_FLAGS -Wl,--cref,-M,-Map=sgit.map)
add_dependencies(sgit-bin sgit-lib)
target_link_libraries(sgit-bin sgit-lib ${LIBS})
